package com.chensf.studydemo.common

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.chensf.studydemo.R
import kotlinx.android.synthetic.main.fragment_image_view_demo.*

class ImageViewPagerDemo : Fragment() {
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_image_view_demo, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        viewPager.offscreenPageLimit = 5
        viewPager.adapter = MyAdapter(this)
    }

    private class MyAdapter(fragment: Fragment) : FragmentStateAdapter(fragment) {
        private val data = arrayOf(
            R.drawable.test1,
            R.drawable.test2,
            R.drawable.test3,
            R.drawable.test4,
            R.drawable.test5,
            R.drawable.test6,
            R.drawable.test7,
            R.drawable.test8,
            R.drawable.test9,
            R.drawable.test10,
            R.drawable.test11,
            R.drawable.test12,
            R.drawable.test13,
            R.drawable.test14,
            R.drawable.test15,
            R.drawable.test16,
            R.drawable.test17
        )
        private val data2 = arrayOf(
            "test1.jpg",
            "test2.jpg",
            "test3.jpg",
            "test4.jpg",
            "test5.jpg",
            "test6.jpg",
            "test7.jpg",
            "test8.jpg",
            "test9.jpg",
            "test10.jpg"
        )
        override fun getItemCount(): Int {
            return data2.size
        }

        override fun createFragment(position: Int): Fragment {
            return ImageFragment.newInstance(data2[position],position)
        }
    }
}